package com.eqdd.yiqidian.ui.gongneng.renshi.kaoqin.classes;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.TimePickerView;
import com.coorchice.library.SuperTextView;
import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.yiqidian.AddClassesActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.MyItemAnimator;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimAddEditBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimTwoSectionEditBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class AddClassesActivity extends SlimBaseMultiEditActivity {
    private AddClassesActivityCustom dataBinding;
    private int clickPos;
    private boolean isUpper;
    @ToSave
    String[] startTimes = new String[]{
            "00:00",
            "00:00",
            "00:00",
            "00:00"
    };
    @ToSave
    String[] endTimes = new String[]{
            "00:00",
            "00:00",
            "00:00",
            "00:00"
    };
    private String msg;
    private SimpleDateFormat sf;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_classes);
        initTopTitleBar(View.VISIBLE, "新班次");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                updateTime();
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_CLASSES)
                        .params("shiftName", setParam(0))
                        .params("companyId", user.getCompanyId())
                        .params("userGuid", user.getGuid())
                        .params("startTime1", startTimes[0])
                        .params("endTime1", endTimes[0])
                        .params("startTime2", startTimes[1])
                        .params("endTime2", endTimes[1])
                        .params("startTime3", startTimes[2])
                        .params("endTime3", endTimes[2])
                        .params("startTime4", startTimes[3])
                        .params("endTime4", endTimes[3])
                        .execute(new DialogCallBack<HttpResult>(AddClassesActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private void updateTime() {
        int size = slimAdapterEx.getData().size();
        startTimes[0] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 1)).getUpContent();
        endTimes[0] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 1)).getDownContent();
        if (slimAdapterEx.getDataItem(size - 2) instanceof SlimTwoSectionEditBean) {
            startTimes[1] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 2)).getUpContent();
            endTimes[1] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 2)).getDownContent();
        } else {
            return;
        }
        if (slimAdapterEx.getDataItem(size - 3) instanceof SlimTwoSectionEditBean) {
            startTimes[2] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 3)).getUpContent();
            endTimes[2] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 3)).getDownContent();
        } else {
            return;
        }
        if (slimAdapterEx.getDataItem(size - 4) instanceof SlimTwoSectionEditBean) {
            startTimes[3] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 4)).getUpContent();
            endTimes[3] = ((SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 4)).getDownContent();
        }
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(slimAdapterEx.getDataItem(0).getContent())) {
            msg = "请填写班次名称";
            return false;
        }
        int size = slimAdapterEx.getData().size();
        while (slimAdapterEx.getDataItem(size - 1) instanceof SlimTwoSectionEditBean) {
            SlimTwoSectionEditBean dataItem = (SlimTwoSectionEditBean) slimAdapterEx.getDataItem(size - 1);
            if (TextUtils.isEmpty(dataItem.getUpContent()) || TextUtils.isEmpty(dataItem.getDownContent())) {
                msg = "班次时间不完整";
                return false;
            }
            size--;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        sf = new SimpleDateFormat("HH:mm");

        DefaultItemAnimator animator = new DefaultItemAnimator();
        animator.setAddDuration(1000);
        animator.setRemoveDuration(1000);
        dataBinding.recyclerView.setItemAnimator(animator);
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        if (slimAdapterEx.getDataItem(p) instanceof SlimAddEditBean) {
            if (slimAdapterEx.getData().size() < 6) {
                slimAdapterEx.getData().add(2, new SlimTwoSectionEditBean("时段" + (slimAdapterEx.getData().size() - 1), "删除", 2, "开始时间",
                        "结束时间", "", "").setShowBottom(true));
                slimAdapterEx.notifyItemInserted(2);
                if (slimAdapterEx.getData().size() == 6) {
                    slimAdapterEx.getData().remove(1);
                    slimAdapterEx.notifyItemRemoved(1);
                }
            }
        }
    }


    @Override
    protected void setEditBeans() {


        slimAdapterEx.register(R.layout.list_item_41, new SlimInjector<SlimAddEditBean>() {
            @Override
            public void onInject(SlimAddEditBean data, IViewInjector injector) {
                injector
                        .text(R.id.tv_content, data.getContent())
                        .tag(R.id.tv_content, injector.pos());
            }
        })
                .register(R.layout.list_item_42, new SlimInjector<SlimTwoSectionEditBean>() {
                    @Override
                    public void onInject(SlimTwoSectionEditBean data, IViewInjector injector) {
                        injector
                                .text(R.id.stv_bottom, data.getContent())
                                .with(R.id.stv_bottom, v -> {
                                    ((SuperTextView) v).setDrawable(getResources().getDrawable(R.mipmap.ic_heart_red));

                                })
                                .text(R.id.tv_upper_left_content, data.getUpTitle())
                                .text(R.id.tv_upper_right_content, data.getUpContent())
                                .text(R.id.tv_down_left_content, data.getDownTitle())
                                .text(R.id.tv_down_right_content, data.getDownContent())
                                .visibility(R.id.stv_bottom, data.isShowBottom() ? View.VISIBLE : View.GONE)
                                .clicked(R.id.stv_bottom, v -> {
                                    int deletePos = slimAdapterEx.getData().indexOf(data);
                                    slimAdapterEx.getData().remove(deletePos);
                                    slimAdapterEx.notifyItemRemoved(deletePos);
                                    if (!(slimAdapterEx.getDataItem(1) instanceof SlimAddEditBean)) {
                                        slimAdapterEx.getData().add(1, new SlimAddEditBean("", "+添加时段", 1));
                                        slimAdapterEx.notifyItemInserted(1);
                                    }

                                })
                                .clicked(R.id.rl_upper, v -> {
                                    clickPos = slimAdapterEx.getData().indexOf(data);
                                    isUpper = true;
                                    openTimePicker();
                                })
                                .clicked(R.id.rl_down, v -> {
                                    clickPos = slimAdapterEx.getData().indexOf(data);
                                    isUpper = false;
                                    openTimePicker();
                                });
                    }
                });

        datas.add(new SlimEditEnterBean("班次名称", "", 0, "请填写"));
        datas.add(new SlimAddEditBean("", "+添加时段", 1));
        datas.add(new SlimTwoSectionEditBean("时段1", "", 2, "开始时间",
                "结束时间", "", "").setShowBottom(false));

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    private void openTimePicker() {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.HOUR_OF_DAY, 0);
        Calendar end = Calendar.getInstance();
        end.set(Calendar.HOUR_OF_DAY, 23);
        TimePickerView pvDayTime = new TimePickerView.Builder(this, (date, v) -> {

            SlimTwoSectionEditBean dataItem = (SlimTwoSectionEditBean) slimAdapterEx.getDataItem(clickPos);

            if (isUpper) {

                dataItem.setUpContent(sf.format(date));
                slimAdapterEx.notifyItemChanged(clickPos);
                int flag = slimAdapterEx.getData().size() - clickPos - 1;
//                startTimes[flag] = dataItem.getUpContent();
            } else {
                dataItem.setDownContent(sf.format(date));
                slimAdapterEx.notifyItemChanged(clickPos);
                int flag = slimAdapterEx.getData().size() - clickPos - 1;
//                endTimes[flag] = dataItem.getDownContent();
            }
        })
                .setOutSideCancelable(true)
                .setType(new boolean[]{false, false, false, true, true, false})
                .setRangDate(start, end)
                .build();
        pvDayTime.show();
    }
}
